#! /bin/bash

#=======================================================================
# the script builds qtopia-2.2.0 embedded environment.
#=======================================================================
#Qt-hisi-2.2.0 includes qtopia-2.2.0 and png,zlib,jpeg,uuid 3rdparties.
#the content of the tar is listed below:
#
#  Qt-hisi-2.2.0/
#  Qt-hisi-2.2.0/doc.txt
#  Qt-hisi-2.2.0/Qtools/
#  Qt-hisi-2.2.0/Qtools/libpng-1.2.14.tar.gz
#  Qt-hisi-2.2.0/Qtools/zlib-1.2.3.tar.gz
#  Qt-hisi-2.2.0/Qtools/qtopia-free-src-2.2.0.tar.gz
#  Qt-hisi-2.2.0/Qtools/jpegsrc.v6b.tar.gz
#  Qt-hisi-2.2.0/Qtools/e2fsprogs-1.39.tar.gz
#  Qt-hisi-2.2.0/ins-libs.sh
#  Qt-hisi-2.2.0/qpe-install.sh
#
#=========================================================================

# common variables,includes cross-toolchains,prefix-dir etc.
QTOOLS_DIR=$PWD

CROSS=/opt/hisi-linux/x86-arm/gcc-3.4.3-uClibc-0.9.28/usr/bin
PLATFORM=arm-hismall-linux
CC=$CROSS/$PLATFORM-gcc
LD=$CROSS/$PLATFORM-ld
AR=$CROSS/$PLATFORM-ar
CPP=$CROSS/$PLATFORM-g++
RANLIB=$CROSS/$PLATFORM-ranlib

# create build directory for 3rd liraries and headers
PREFIX=$PWD/build
PREFIX_LIB=$PREFIX/lib
PREFIX_INC=$PREFIX/include

mkdir -p $PREFIX_LIB $PREFIX_INC

# create install directory for qtopia installation
INSTALL=$PWD/install
mkdir -p $INSTALL

BUILD_LOG=$PWD/3rd_build.log

echo "=======Begin to build 3rd libraries...======="

echo "*********start to build e2fsprogs(uuid)*********"
echo "*********start to build e2fsprogs(uuid)*********" > $BUILD_LOG
tar -zxf Qtools/e2fsprogs-1.39.tar.gz
pushd e2fsprogs-1.39
./configure --host=$PLATFORM --enable-elf-shlibs --with-cc=$CC --with-linker=$LD --prefix=$PREFIX
make 2>>$BUILD_LOG
cp -fv lib/libuuid.so* $PREFIX_LIB  
cp -rfv lib/uuid $PREFIX_INC 
popd
echo "*********e2fsprogs build successfull*********"

echo "*********start to build jpegsrc.v6b**********"
echo "*********start to build jpegsrc.v6b**********" >> $BUILD_LOG
tar -zxf Qtools/jpegsrc.v6b.tar.gz
pushd jpeg-6b
mkdir -pv install/bin install/include install/lib install/man/man1
./configure --enable-shared --prefix=$PWD/install
echo y | cp jconfig.doc jconfig.h
# extract JPEG library version for libtool utility
JPEG_LIB_VER=`sed -n '/#define JPEG_LIB_VERSION/s/[[:space:]][[:space:]]*/ /gp' jpeglib.h |cut -d " " -f 3`
make CC=$CC AR="$AR rc" AR2=$RANLIB JPEG_LIB_VERSION=$JPEG_LIB_VER 2>>$BUILD_LOG
make install 
cp -fv install/lib/libjpeg.* $PREFIX_LIB 
cp -fv j*.h $PREFIX_INC 
cp -fv install/man $PREFIX/
popd 
echo "*********jpeg build successful*********"
                                                                            
echo "*********start to build zlib***********"
echo "*********start to build zlib***********" >> $BUILD_LOG
tar -zxf Qtools/zlib-1.2.3.tar.gz
pushd zlib-1.2.3
./configure -shared
make CC=$CC LDSHARED="$CC -shared -Wl,-soname,libz.so.1" CPP="$CC -E" AR="$AR rc" RANLIB=$RANLIB prefix=$PREFIX 2>>$BUILD_LOG
cp -fv libz.so* $PREFIX_LIB
cp -fv *.h $PREFIX_INC
popd
echo "*********zlib build successful*********"

echo "*********start to build libpng*********"
echo "*********start to build libpng*********" >> $BUILD_LOG
tar -zxf Qtools/libpng-1.2.14.tar.gz
mv -fv zlib-1.2.3 zlib
pushd libpng-1.2.14
cp scripts/makefile.linux ./Makefile
make CC=$CC AR_RC="$AR rc" RANLIB=$RANLIB prefix=$PREFIX 2>>$BUILD_LOG
cp -fv libpng12.so* $PREFIX_LIB
# remove the libpng version number to let ld find it
#ln -s $PREFIX_LIB/libpng12.so.0 $PREFIX_LIB/libpng.so.0
cp -fv $PREFIX_LIB/libpng12.so.0 $PREFIX_LIB/libpng.so
cp -fv *.h $PREFIX_INC
popd 
mv -fv zlib zlib-1.2.3
echo "*********libpng build successful*********"

echo "=======End to build 3rd libraries.======="

echo "=======Begin to build qtopia-2.2.0======="

echo "-------step1:create platform tmake.conf and qmake.conf-------"
tar -zxf Qtools/qtopia-free-src-2.2.0.tar.gz
pushd qtopia-free-2.2.0
QT_ROOT_DIR=$PWD
QT_BUILD_LOG=$QT_ROOT_DIR/build.log

# create platform tmake qws config file
cp -frv tmake/lib/qws/linux-arm-g++ tmake/lib/qws/arm-hismall-linux-g++
pushd tmake/lib/qws/arm-hismall-linux-g++ 
# modify tmake.conf file
sed -i 's/arm-linux/arm-hismall-linux/g' tmake.conf
popd 

# create platform qtopia mkspecs qws files 
cp -frv qtopia/mkspecs/qws/linux-arm-g++/ qtopia/mkspecs/qws/arm-hismall-linux-g++ 
pushd qtopia/mkspecs/qws/arm-hismall-linux-g++
# modify qmake.conf file
sed -i 's/arm-linux/arm-hismall-linux/g' qmake.conf
sed -i '61c\QMAKE_LIBS_QT = -lqte -lpng -lz -luuid -ljpeg' qmake.conf 
popd

echo "-------step 2: create qconfig-qpe.h file for our project-------"
# modify qconfig-xxx.h 
pushd qtopia/src/qt
#if let qtopia support mouse
#/*
#ifndef QT_NO_QWS_CURSOR
#define QT_NO_QWS_MOUSE_PC
#endif
#*/
sed -i '47a\/* ' qconfig-qpe.h
sed -i '60i\*/ ' qconfig-qpe.h

# used for qte build
cp qconfig-qpe.h $QT_ROOT_DIR/qt2/src/tools
popd

# about touch panels feature
pushd qtopia/src/libraries/qtopia
# iPAQ Touch Panel
#cp custom-linux-ipaq-g++.cpp custom-linux-arm-g++.cpp
#cp custom-linux-ipaq-g++.h custom-linux-arm-g++.h
# Don't use Touch Panel
cp custom-linux-x86-g++.cpp custom-$PLATFORM-g++.cpp
cp custom-linux-x86-g++.h custom-$PLATFORM-g++.h
popd

echo "-------step 3:fix source files compile errors-------"
pushd $QTOOLS_DIR
./fix-src-errors.sh
popd

echo "-------step 4:configure,make and install qtopia-------"
QTE_CFG="'-embedded -no-xft -xplatform $PLATFORM-g++ -qconfig qpe -depths 16,32 -no-qvfb -system-jpeg -gif -system-libpng -I$PREFIX_INC -lpng -lz -ljpeg -L$PREFIX_LIB'"
QPE_CFG="'-xplatform $PLATFORM-g++ -edition pda -displaysize 240x320'"
QT2_CFG=
DQT_CFG=

# configure
echo "yes" |./configure -qte "$QTE_CFG" -qpe "$QPE_CFG" -libpath $PREFIX_LIB -prefix $INSTALL > $QT_BUILD_LOG

# set environment variables
if [ -f setQpeEnv ]
then
	. ./setQpeEnv
	export PATH=$TMAKEDIR/bin:$PATH
fi

# make
make 2>>$QT_BUILD_LOG

# install
make install

popd

